#include "connection.hpp"

#include <QDebug>
#include <QCryptographicHash>

quint16 Connection::port()
{
    return _port->value();
}

QString Connection::pseudo()
{
    return _pseudo->text();
}

QString Connection::hash()
{
    return QString(QCryptographicHash::hash(QByteArray(_password->text().toStdString().c_str()), QCryptographicHash::Sha1).constData());
}

Connection::Connection(QWidget *parent) : QWidget(parent)
{
    setWindowTitle("Connection");
    _layout = new QGridLayout(this);
    setLayout(_layout);

    _labelPort = new QLabel("Port", this);
    _labelIP = new QLabel("IP", this);
    _labelPseudo= new QLabel("Login", this);
    _labelPassword = new QLabel("Password", this);
    _port = new QSpinBox(this);
    _IP = new QLineEdit(this);
    _pseudo = new QLineEdit(this);
    _password = new QLineEdit(this);
    _button = new QPushButton("Log in", this);

    _port->setRange(1024, 65535);
    _port->setValue(4242);

    _password->setEchoMode(QLineEdit::Password);

    _IP->setInputMask("000.000.000.000;_");

    _layout->addWidget(_labelIP, 0, 0);
    _layout->addWidget(_IP, 0, 1);
    _layout->addWidget(_labelPort, 1, 0);
    _layout->addWidget(_port, 1, 1);
    _layout->addWidget(_labelPseudo, 2, 0);
    _layout->addWidget(_pseudo, 2, 1);
    _layout->addWidget(_labelPassword, 3, 0);
    _layout->addWidget(_password, 3, 1);
    _layout->addWidget(_button, 4, 0, 1, 2, Qt::AlignCenter);

    connect(_button, SIGNAL(clicked()), this, SLOT(reactClick()));

    resize(400,400);
}

void Connection::reactClick()
{
    _button->setEnabled(false);
    emit connection(_IP->text(), _port->value());
}

void Connection::debloquerBouton()
{
    _button->setEnabled(true);
}
